package com.example.boot1.conttoller;
import com.example.boot1.bean.BackDataBean;
import com.example.boot1.bean.BackPageDataBean;
import com.example.boot1.bean.VueCuisine;
import com.example.boot1.service.VueCuisineService;
import com.example.boot1.util.DateUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
* (vue_cuisine)表控制层
*
* @author chen feilong
*/
@RestController
@RequestMapping("/vue")
public class VueCuisineController {
/**
* 服务对象
*/
    @Autowired
    private VueCuisineService vueCuisineService;


    @PostMapping("/subCuisine")
    @ResponseBody
    public ResponseEntity<BackDataBean> subCuisine(HttpServletRequest request,
                                                @RequestBody VueCuisine vueCuisine){

        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        //前端输入校验
        //菜目类别
        if (null==vueCuisine.getDishId()  || "".equals(vueCuisine.getDishId())){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入菜的类别！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            return  responseEntity;
        }
        //菜品名称
        if (null==vueCuisine.getName()  || "".equals(vueCuisine.getName())){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入名称！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            return  responseEntity;
        }
        //菜品价格
        if (null==vueCuisine.getUnitprice()  || vueCuisine.getUnitprice().equals("") || vueCuisine.getUnitprice().floatValue() <= 0){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入菜品价格！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            return  responseEntity;
        }
        //菜品单位
        if (null==vueCuisine.getUnit()  || vueCuisine.getUnit().equals("") ){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入菜品单位！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            return  responseEntity;
        }
        //菜品图片
        if (null==vueCuisine.getGoodsimage()  || vueCuisine.getGoodsimage().size()==0){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请上传菜品图片！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            return  responseEntity;
        }
        //插入逻辑
        try {

            List<VueCuisine> vlist = vueCuisineService.findByName(vueCuisine.getName());

            if ( null != vlist && vlist.size()>0){
                backDataBean = new BackDataBean();
                backDataBean.setCode(202);
                backDataBean.setMsg("菜名已存在！");
                backDataBean.setResult(null);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }else{

                int utList = vueCuisineService.insertSelective(vueCuisine);

                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("保存成功！");
                backDataBean.setResult(utList);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }
        }catch (Exception e) {

            backDataBean.setCode(202);
            backDataBean.setMsg("服务器错误！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            e.printStackTrace();
        }
        return responseEntity;
    }

    /**
     * token 未实现，如实现参考:VueUserController VueOrderController
     * @param request
     * @param page
     * @return
     */
    @GetMapping("/getCuisine")
    @ResponseBody
    public ResponseEntity<BackDataBean> getCuisine(HttpServletRequest request, Integer page){
        ResponseEntity responseEntity = null;
        BackPageDataBean backPageDataBean = null;
        BackDataBean backDataBean = null;

        int pageOffSetNum = page * 10;
        List<VueCuisine> vclist = vueCuisineService.getAllCuisine(request,pageOffSetNum);

        if (null!= vclist &&  vclist.size()>0){

            backDataBean = new BackDataBean();
            backDataBean.setCode(200);
            backDataBean.setMsg("查询成功！");
            backDataBean.setResult(vclist);

            backPageDataBean = new BackPageDataBean(backDataBean);
            backPageDataBean.setPage(page);
            backPageDataBean.setTotal(vueCuisineService.findAllTotal());
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backPageDataBean);

        }else{
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("没有数据！");
            backDataBean.setResult(null);

            backPageDataBean = new BackPageDataBean(backDataBean);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backPageDataBean);
        }

        return responseEntity;
    }


    @GetMapping("/isSoldOut")
    @ResponseBody
    public ResponseEntity<BackDataBean> isSoldOut(HttpServletRequest request, Integer id){
        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        //查找
        VueCuisine vc = vueCuisineService.selectByPrimaryKey(id);
        if (null != vc){
            //
            vc.setOnsale(false);
            //修改
            int resultNum=vueCuisineService.updateByPrimaryKeySelective(vc);

            backDataBean = new BackDataBean();
            backDataBean.setCode(200);
            backDataBean.setMsg("保存成功！");
            backDataBean.setResult(resultNum);
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);

        }else{
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("服务器错误！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
        }
        return responseEntity;
    }

    @PostMapping("/editCuisine")
    @ResponseBody
    public ResponseEntity<BackDataBean> editCuisine(HttpServletRequest request,
                                                @RequestBody VueCuisine vueCuisine){

        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        //前端输入校验
        //菜目类别
        if (null==vueCuisine.getDishId()  || "".equals(vueCuisine.getDishId())){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入菜的类别！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //菜品名称
        if (null==vueCuisine.getName()  || "".equals(vueCuisine.getName())){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入名称！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //菜品价格
        if (null==vueCuisine.getUnitprice()  || vueCuisine.getUnitprice().equals("") || vueCuisine.getUnitprice().floatValue() <= 0){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入菜品价格！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //菜品单位
        if (null==vueCuisine.getUnit()  || vueCuisine.getUnit().equals("") ){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入菜品单位！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //菜品图片
        if (null==vueCuisine.getGoodsimage()  || vueCuisine.getGoodsimage().size()==0){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请上传菜品图片！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //修改逻辑
        try {
            //检查除了id相同职外还存在相同的名称的对象
            List<VueCuisine> vlist = vueCuisineService.findByIdAndName(vueCuisine.getId(),vueCuisine.getName());

            if ( null != vlist && vlist.size()>0){
                backDataBean = new BackDataBean();
                backDataBean.setCode(202);
                backDataBean.setMsg("菜名已存在！");
                backDataBean.setResult(null);
                responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);

            }else{

                int utList = vueCuisineService.updateByPrimaryKeySelective(vueCuisine);

                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("修改成功！");
                backDataBean.setResult(utList);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }
        }catch (Exception e) {

            backDataBean.setCode(202);
            backDataBean.setMsg("服务器错误！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            e.printStackTrace();
        }
        return responseEntity;
    }

}
